package com.lmpop.trade.uploadFile;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ReturnImgWH
{
  public int[] returnSrcImgWH(String img_path)
  {
    File file = new File(img_path);
    int[] a = new int[2];
    if (file.exists())
    {
      BufferedImage bi = null;
      boolean imgwrong = false;
      try
      {
        bi = ImageIO.read(file);
        try {
          int i = bi.getType();
          imgwrong = true;
        } catch (Exception e) {
          imgwrong = false;
        }
      } catch (IOException ex) {
        ex.printStackTrace();
      }
      if (imgwrong) {
        a[0] = bi.getWidth();
        a[1] = bi.getHeight();
      } else {
        a[0] = 0;
        a[1] = 0;
      }
    } else {
      a[0] = 0;
      a[1] = 0;
    }
    return a;
  }

  public int[] returnDisImgWH(int[] a, int setWidth, int setHeight)
  {
    int[] b = new int[2];

    int srcWidth = a[0];
    int srcHeight = a[1];

    if (a[0] == 0) {
      b[0] = setWidth;
      b[1] = setHeight;
      return b;
    }

    if ((srcWidth > setWidth) || (srcHeight > setHeight))
    {
      if (srcWidth > srcHeight) {
        float imgHeight_f = srcHeight / srcWidth * setHeight;
        srcWidth = setWidth;
        srcHeight = (int)imgHeight_f;
      } else {
        float imgWidth_f = srcWidth / srcHeight * setWidth;
        srcWidth = (int)imgWidth_f;
        srcHeight = setHeight;
      }

    }

    int retWidth = srcWidth;
    int retHeight = srcHeight;

    b[0] = retWidth;
    b[1] = retHeight;

    return b;
  }

  public static void main(String[] args)
  {
    ReturnImgWH ret = new ReturnImgWH();
    int[] a = ret.returnSrcImgWH("c://3333333.bmp");

    System.out.println(a[0] + "====" + a[1]);

    int[] b = ret.returnDisImgWH(a, 140, 120);
    System.out.println(b[0] + "====" + b[1]);
  }
}